import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.db.session import engine, Base
from app.models import user, graph
from sqlalchemy import text

def migrate():
    print("开始迁移数据库...")
    try:
        with engine.connect() as conn:
            # 删除并重新创建数据库
            print("删除并重新创建数据库...")
            conn.execute(text("DROP DATABASE IF EXISTS knowledge_graph;"))
            conn.execute(text("CREATE DATABASE knowledge_graph CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"))
            conn.execute(text("USE knowledge_graph;"))
            
            # 创建所有表
            print("创建新表...")
            Base.metadata.create_all(bind=engine)

        print("数据库迁移完成！")
    except Exception as e:
        print(f"迁移失败: {e}")

if __name__ == "__main__":
    migrate() 